﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SGD;

namespace ParticleEditor
{
    public partial class Form1 : Form
    {
        private ParticleEmitter Emitter = new ParticleEmitter();
    

        private OpenFileDialog textureFileDialog = new OpenFileDialog();
        private ColorDialog StartColorDialog = new ColorDialog();
        private ColorDialog EndColorDialog = new ColorDialog();

        public Random randNum = new Random();

        private System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch();

        private int particleImageID = -1;

        public Point centerPoint;

       

        private int sourceBlend;
        private int destBlend;
        public bool adjustment;

        public Form1()
        {
            InitializeComponent();
            centerPoint = new Point(splitContainer1.Panel2.Width >> 1, splitContainer1.Panel2.Height >> 1);
            ManagedDirect3D.Instance.InitManagedDirect3D(splitContainer1.Panel2, true);
            ManagedTextureManager.Instance.InitManagedTextureManager(ManagedDirect3D.Instance.Device, ManagedDirect3D.Instance.Sprite);

            StartColorDialog.Color = Color.White;
            EndColorDialog.Color = Color.White;
            button1.BackColor = Color.White;
            button2.BackColor = Color.White;

#if DEBUG
            string defaultname = Environment.CurrentDirectory + "\\..\\..\\ParticlePic1.png";
#else
            string defaultname = Environment.CurrentDirectory + "\\ParticlePic1.png";

#endif
            adjustment = false;
            stopWatch.Start();

            Emitter.CreateParticleEmitter();

            particleImageID = ManagedTextureManager.Instance.LoadTexture(defaultname, 0);

            sourceBlend = 0;
            destBlend = 0;

            Emitter.Resize((int)numericUpDown3.Value);

            Emitter.StartEmitter();



        }

        public void SetNumeric(float x, float y)
        {
            numericUpDown4.Value = (int)x;
            numericUpDown5.Value = (int)y;

        }

        public void Update(float dt)
        {
            Emitter.Update(dt);
        }

        public void Render()
        {
            ManagedDirect3D.Instance.Clear(0, 0, 0);
            ManagedDirect3D.Instance.DeviceBegin();
            ManagedDirect3D.Instance.SpriteBegin();

            if (sourceBlend > 0)
            {
                ManagedDirect3D.Instance.Device.SetRenderState(Microsoft.DirectX.Direct3D.RenderStates.SourceBlend, sourceBlend);
            }
            if (destBlend > 0)
            {
                ManagedDirect3D.Instance.Device.SetRenderState(Microsoft.DirectX.Direct3D.RenderStates.DestinationBlend, destBlend);
            }

            Emitter.Render(particleImageID, centerPoint);

            ManagedDirect3D.Instance.SpriteEnd();
            ManagedDirect3D.Instance.DeviceEnd();
            ManagedDirect3D.Instance.Present();
        }

        private void loadImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (textureFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filepath = textureFileDialog.FileName;
                particleImageID = ManagedTextureManager.Instance.LoadTexture(filepath, 0);
                Emitter.ParticleID = particleImageID;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                Emitter.IsLooping = true;
            }
        }

        private void numericUpDown3_ValueChanged(object sender, EventArgs e)
        {
            Emitter.Resize((int)numericUpDown3.Value);
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            sourceBlend = comboBox1.SelectedIndex + 1;
            Emitter.SourceBlend = sourceBlend;
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            destBlend = comboBox2.SelectedIndex + 1;
            Emitter.DestinationBlend = destBlend;
        }

        private void numericUpDown12_ValueChanged(object sender, EventArgs e)
        {
            Emitter.ParticleDelay = ((int)numericUpDown12.Value);
        }

        private void numericUpDown14_ValueChanged(object sender, EventArgs e)
        {
            Emitter.Scale_s = ((int)numericUpDown14.Value);
        }

        private void numericUpDown15_ValueChanged(object sender, EventArgs e)
        {
            Emitter.Scale_f = ((int)numericUpDown15.Value);
        }


        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {
            Emitter.ParticleLifetime = ((float)numericUpDown1.Value);
        }

        private void numericUpDown4_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown6.Value = numericUpDown4.Value;

            Emitter.VelX_s = (float)numericUpDown4.Value;
        }

        private void numericUpDown5_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown7.Value = numericUpDown5.Value;

            Emitter.VelY_s = (float)numericUpDown5.Value;
        }

        private void numericUpDown6_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown4.Value = numericUpDown6.Value;

            Emitter.VelX_f = (float)numericUpDown6.Value;
        }

        private void numericUpDown7_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown5.Value = numericUpDown7.Value;

            Emitter.VelY_f = (float)numericUpDown7.Value;
        }

        private void splitContainer1_Panel2_Resize(object sender, EventArgs e)
        {
            centerPoint = new Point(splitContainer1.Panel2.Width >> 1, splitContainer1.Panel2.Height >> 1);            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            StartColorDialog.ShowDialog();
            button1.BackColor = StartColorDialog.Color;
            Emitter.Start1 = StartColorDialog.Color;
            Emitter.Alpha_s = (int)StartTransparencyUpDown.Value;
            Emitter.Alpha_f = (int)EndTransparencyUpDown.Value;
        }

        private void numericUpDown11_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown9.Value = -numericUpDown11.Value;

            Emitter.PosX_s = (float)numericUpDown11.Value;
        }

        private void numericUpDown10_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown8.Value = -numericUpDown10.Value;

            Emitter.PosY_s = (float)numericUpDown10.Value;
        }

        private void numericUpDown9_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown11.Value = -numericUpDown9.Value;

            Emitter.PosX_f = (float)numericUpDown9.Value;
        }

        private void numericUpDown8_ValueChanged(object sender, EventArgs e)
        {
            if (adjustment)
                numericUpDown10.Value = -numericUpDown8.Value;

            Emitter.PosY_f = (float)numericUpDown8.Value;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            EndColorDialog.ShowDialog();
            button2.BackColor = EndColorDialog.Color;
            Emitter.Finish1 = EndColorDialog.Color;
            Emitter.Alpha_s = (int)StartTransparencyUpDown.Value;
            Emitter.Alpha_f = (int)EndTransparencyUpDown.Value;
        }

        private void StartTransparencyUpDown_ValueChanged(object sender, EventArgs e)
        {
            Emitter.Alpha_s1 = (int)StartTransparencyUpDown.Value;
        }

        private void EndTransparencyUpDown_ValueChanged(object sender, EventArgs e)
        {
            Emitter.Alpha_f1 = (int)EndTransparencyUpDown.Value;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            adjustment = !adjustment;
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox3.SelectedIndex)
            {
                case 0:
                    Emitter.vCircleBased = true;
                    Emitter.vTimeBase = false;
                    Emitter.vPointBase = false;
                    break;

                case 1:
                    Emitter.vCircleBased = false;
                    Emitter.vTimeBase = false;
                    Emitter.vPointBase = true;
                    break;

                case 2:
                    Emitter.vCircleBased = false;
                    Emitter.vTimeBase = true;
                    Emitter.vPointBase = false;
                    break;

                default:
                    Emitter.vCircleBased = false;
                    Emitter.vTimeBase = false;
                    Emitter.vPointBase = true;
                    break;
 
            }
        }



    }
}
